package org.dromara.erp.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.erp.domain.ErpFarmSaleInfo;
import org.dromara.erp.service.IErpFarmSaleInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 销售明细
 *
 * @author Mr Lei QQ:235921049
 * @date 2024-06-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/erp/farmSaleInfo")
public class ErpFarmSaleInfoController extends BaseController {

    private final IErpFarmSaleInfoService erpFarmSaleInfoService;

    /**
     * 查询销售明细列表
     */
    @SaCheckPermission("erp:farmSaleInfo:list")
    @GetMapping("/list")
    public TableDataInfo<ErpFarmSaleInfo> list(ErpFarmSaleInfo bo, PageQuery pageQuery) {
        return erpFarmSaleInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出销售明细列表
     */
    @SaCheckPermission("erp:farmSaleInfo:export")
    @Log(title = "销售明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpFarmSaleInfo bo, HttpServletResponse response) {
        List<ErpFarmSaleInfo> list = erpFarmSaleInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "销售明细", ErpFarmSaleInfo.class, response);
    }

    /**
     * 获取销售明细详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("erp:farmSaleInfo:query")
    @GetMapping("/{id}")
    public R<ErpFarmSaleInfo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(erpFarmSaleInfoService.queryById(id));
    }

    /**
     * 新增销售明细
     */
    @SaCheckPermission("erp:farmSaleInfo:add")
    @Log(title = "销售明细", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpFarmSaleInfo bo) {
        return toAjax(erpFarmSaleInfoService.insertByBo(bo));
    }

    /**
     * 修改销售明细
     */
    @SaCheckPermission("erp:farmSaleInfo:edit")
    @Log(title = "销售明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpFarmSaleInfo bo) {
        return toAjax(erpFarmSaleInfoService.updateByBo(bo));
    }

    /**
     * 删除销售明细
     *
     * @param ids 主键串
     */
    @SaCheckPermission("erp:farmSaleInfo:remove")
    @Log(title = "销售明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(erpFarmSaleInfoService.deleteWithValidByIds(List.of(ids), true));
    }
}
